package com.salt.modules.sys.web;

import com.salt.common.json.AjaxJson;
import com.salt.common.utils.StringUtils;
import com.salt.core.web.BaseController;
import com.salt.modules.sys.entity.Menu;
import com.salt.modules.sys.entity.Office;
import com.salt.modules.sys.entity.RightNavBar;
import com.salt.modules.sys.entity.User;
import com.salt.modules.sys.service.MultiTenantService;
import com.salt.modules.sys.service.RightNavBarService;
import com.salt.modules.sys.utils.UserUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author salt
 * @Date 2022/1/7
 * @Version 1.0.1
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/right/navbar")
public class RightNavBarController extends BaseController {

    private final RightNavBarService rightNavBarService;
    private final MultiTenantService multiTenantService;

    @ModelAttribute("rightNavBar")
    public RightNavBar get(@RequestParam(required = false) String id){
        if (StringUtils.isNotBlank(id)) {
            return rightNavBarService.get(id);
        }
        return new RightNavBar();
    }

    @GetMapping("list")
    public AjaxJson list(RightNavBar rightNavBar) {
        return AjaxJson.success().put("list", rightNavBarService.findList(rightNavBar));
    }

    /**
     * 应用商店左边菜单栏
     * @return
     */
    @GetMapping("rightMenus")
    public AjaxJson rightMenus(Office office) {
        Map map = multiTenantService.getTenant(office);
        String tenant = (String) map.get("tenant");
        List<String> tenantIds = (List<String>) map.get("tenantIds");
        return AjaxJson.success()
                .put("rightMenus", rightNavBarService.rightMenus(new RightNavBar(tenant),
                        new Menu(tenant, tenantIds, "2")));
    }

    /**
     * 应用商店左边菜单栏--全部子应用
     * @return
     */
    @GetMapping("allChildMenus")
    public AjaxJson allChildMenus(Office office) {
        Map map = multiTenantService.getTenant(office);
        Menu menu = new Menu();
        menu.setClient("2");
        menu.setTenant((String) map.get("tenant"));
        menu.setTenantIds((List<String>) map.get("tenantIds"));
        return AjaxJson.success().put("allChildMenus", rightNavBarService.allChildMenus(menu));
    }

    @PostMapping("save")
    public AjaxJson save(RightNavBar rightNavBar) {
        // 添加租户ID
        // 当前用户是超级管理员，则菜单的租户ID标识已在前端添加，
        // 当前用户不是超级管理员，则菜单的租户ID标识取所属公司ID
        User user = UserUtils.getUser();
        if (!user.isAdmin()) {
            rightNavBar.setTenant(user.getCompany().getId());
        }
        rightNavBarService.save(rightNavBar);
        return AjaxJson.success("保存成功");
    }

    @GetMapping("quryById")
    public AjaxJson quryById(RightNavBar rightNavBar) {
        return AjaxJson.success().put("rightNavBar", rightNavBar);
    }

    @DeleteMapping("delete")
    public AjaxJson delete(RightNavBar rightNavBar) {
        if (StringUtils.isNotBlank(rightNavBar.getId())){
            rightNavBarService.delete(rightNavBar);
            return AjaxJson.success("删除成功!");
        }
        return AjaxJson.error("删除失败!");
    }

}
